/**
 * Created by shauan on 18/03/14.
 */

jQuery('document').ready(function(){

    $path_url = jQuery('base').prop('href');
    jQuery("#sa-tab-Grupo").click(function(){
        $active = jQuery(this).parent().prop('class');
        if(($active != 'active') && ($active == '') || ($active == null)){
            ajaxGrupo();
        }
    });

    jQuery("#sa-tab-Categoria").click(function(){
        $active = jQuery(this).parent().prop('class');
        if(($active != 'active') && ($active == '') || ($active == null)){
            ajaxCategoria();
        }
    });

    jQuery("#sa-btn-newAccount").click(function(){
    });

    jQuery("#ClientTipoPessoa").change(function(){
        $valor = jQuery(this).val();
        if( $valor == "F"){
            jQuery("#sa-box-cpf").show();
            jQuery("#sa-box-cnpj").hide();
        }else if($valor == "J"){
            jQuery("#sa-box-cnpj").removeClass('hide').show();
            jQuery("#sa-box-cpf").hide();
        }else{
            jQuery("#sa-box-cpf").show();
            jQuery("#sa-box-cnpj").hide();
        }
    });

});

function alertGritter(title,message,type){
    jQuery.gritter.add({
        title: title,
        text: message,
        class_name: 'growl-'+type,
        sticky: false,
        time: ''
    });
}

function ajaxGrupo(){
    $url = $path_url+'/categoryGroups/showGroups';
    jQuery.ajax({
        url: $url,
        beforeSend:function(){},
        success:function(data){
            jQuery('#grupo').html('').html(data);
        },
        error:function(){},
        complete:function(){}
    });
}

function ajaxCategoria(){
    $url = $path_url+'/categories/showCategories';
    jQuery.ajax({
        url: $url,
        beforeSend:function(){},
        success:function(data){
            jQuery('#categoria').html('').html(data);
        },
        error:function(){},
        complete:function(){}
    });
}

function saBtnConcluirGrupo(){
    $dataForm = jQuery("#CategoryGroupAddForm").serialize();
    $url = $path_url+'/categoryGroups/addGroups';
    $fieldName = jQuery("#CategoryGroupName").val();

    if($fieldName != ''){
        jQuery.ajax({
            type: 'post',
            url:$url,
            dataType:'json',
            data:$dataForm,
            beforeSend:function(){},
            success:function(data){
                alertGritter(data.alert.titulo,data.alert.mensagem,data.alert.tipo);
                return false;
            },
            error:function(data){
                alertGritter(data.alert.titulo,data.alert.mensagem,data.alert.tipo);
                return false;
            },
            complete:function(){
                ajaxGrupo();
            }
        });
    }else{
        alert('Informe o nome do grupo.');
        return false;
    }
}

function saBtnConcluirCategoria(){
    $dataForm = jQuery("#CategoryForm").serialize();
    $url = $path_url+'/categories/addCategories';
    jQuery.ajax({
        type: 'post',
        url:$url,
        dataType:'json',
        data:$dataForm,
        beforeSend:function(){},
        success:function(data){
            alertGritter(data.alert.titulo,data.alert.mensagem,data.alert.tipo);
            return false;
        },
        error:function(data){
//                alert(JSON.stringify(data)); // Debug do Json
            if((data.responseJSON.code != 404) && (data.responseJSON.code ==null)){
                alertGritter(data.alert.titulo,data.alert.mensagem,data.alert.tipo);
            }else{
                alertGritter('ERROR',data.responseJSON.name,'danger');
            }
            return false;
        },
        complete:function(){
            ajaxCategoria();
        }
    });
}

function showNewExpenses(){
    $url = $path_url+'/homes/showNewExpenses';
    jQuery.ajax({
        url: $url,
        beforeSend:function(){},
        success:function(data){
            jQuery('#newAccount').html('').html(data);
        },
        error:function(){},
        complete:function(){}
    });
}

function openModalPagamento(){
    $path = jQuery(this).prop('data-path');
    jQuery('#geralModal').addClass('bs-example-modal-sm');
    showModal();
    $url = $path_url+'/contasPagar/pagarConta/7';
    jQuery.ajax({
        url: $url,
        beforeSend:function(){},
        success:function(data){
            jQuery('#geralModal .modal-content').html('').html(data);
        },
        error:function(){},
        complete:function(){}
    });
}

function showModal(){
    jQuery('#geralModal').modal({
        backdrop:'static',
        keyboard:false
    });
}